package com.cac.demo.newCode.sorting.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cac.demo.newCode.sorting.dto.SupportingTasksDTO;
import com.cac.demo.newCode.sorting.entity.MsAoMatchBill;
import com.cac.demo.newCode.sorting.entity.MsTaskAo;
import com.cac.demo.newCode.sorting.entity.MsTaskPackage;
import com.cac.demo.newCode.sorting.service.IMsAoMatchBillService;
import com.cac.demo.newCode.sorting.service.IMsTaskAoService;
import com.cac.demo.newCode.sorting.service.IMsTaskPackageService;
import com.cac.demo.newCode.sorting.service.SupportingTaskService;
import com.cac.demo.util.adapter.ResponseMsg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SupportingTaskServiceImpl implements SupportingTaskService {

    @Resource
    private IMsTaskPackageService iMsTaskPackageService;
    @Resource
    private IMsTaskAoService iMsTaskAoService;
    @Resource
    private IMsAoMatchBillService iMsAoMatchBillService;

    @Override
    public ResponseMsg<Object> PackagePage(SupportingTasksDTO supportingTasksDTO) {
        Page<MsTaskPackage> page = new Page(supportingTasksDTO.getCurrent(), supportingTasksDTO.getSize());
        LambdaQueryWrapper<MsTaskPackage> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(supportingTasksDTO.getProductCode())) {
            queryWrapper.like(MsTaskPackage::getProductCode, supportingTasksDTO.getProductCode());
        }
        if (StringUtils.isNotEmpty(supportingTasksDTO.getBatchNo())) {
            queryWrapper.like(MsTaskPackage::getBatchNo, supportingTasksDTO.getBatchNo());
        }
        if (StringUtils.isNotEmpty(supportingTasksDTO.getLotNo())) {
            queryWrapper.like(MsTaskPackage::getStartPlaneNo, supportingTasksDTO.getLotNo());
        }
        if(supportingTasksDTO.getStartTime()!=null&&supportingTasksDTO.getStartTime()!=""&&supportingTasksDTO.getEndTime()!=null&&supportingTasksDTO.getEndTime()!=""){
            queryWrapper.apply("required_time>=TO_DATE('" + supportingTasksDTO.getStartTime() + "','YYYY-MM-DD')");
            queryWrapper.apply("required_time<=TO_DATE('" + supportingTasksDTO.getEndTime() + "','YYYY-MM-DD')");
        }
        page = iMsTaskPackageService.page(page, queryWrapper);
        return new ResponseMsg<>().setResponseBody(page);
    }

    @Override
    public ResponseMsg<Object> AOPage(SupportingTasksDTO supportingTasksDTO) {
        Page<MsTaskAo> page = new Page(supportingTasksDTO.getCurrent(), supportingTasksDTO.getSize());
        LambdaQueryWrapper<MsTaskAo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsTaskAo::getMsTaskPackageId, supportingTasksDTO.getMsTaskPackageId());
        page = iMsTaskAoService.page(page, queryWrapper);
        return new ResponseMsg<>().setResponseBody(page);
    }

    @Override
    public ResponseMsg<Object> AOMaterialPage(SupportingTasksDTO supportingTasksDTO) {
        Page<MsAoMatchBill> page = new Page(supportingTasksDTO.getCurrent(), supportingTasksDTO.getSize());
        LambdaQueryWrapper<MsAoMatchBill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MsAoMatchBill::getMsTaskAoId, supportingTasksDTO.getMsTaskAoId());
        page = iMsAoMatchBillService.page(page, queryWrapper);
        return new ResponseMsg<>().setResponseBody(page);
    }
}
